package com.koron.sinoprobe.web.plan.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.Response;
import com.koron.bean.system.staff.vo.EamUser;
import com.koron.bean.workflow.RedirectQuery;
import com.koron.bean.workflow.SubmitterParam;
import com.koron.bean.workflow.WorkflowController;
import com.koron.bean.workflow.WorkflowUser;
import com.koron.common.core.annocation.CasUser;
import com.koron.common.core.business.system.feign.SystemFeignService;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.common.core.interceptor.OrderByInterceptor;
import com.koron.sinoprobe.web.plan.bean.query.SpPlanPackageQuery;
import com.koron.sinoprobe.web.plan.bean.query.SpPlanQuery;
import com.koron.sinoprobe.web.plan.bean.vo.SpPlanPackageVO;
import com.koron.sinoprobe.web.plan.bean.vo.SpPlanVO;
import com.koron.sinoprobe.web.plan.filter.SpPlanPackageFilter;
import com.koron.sinoprobe.web.plan.service.SpPlanPackageService;
import com.koron.sinoprobe.web.plan.service.SpPlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

@Deprecated
@Slf4j
@RestController
@RequestMapping("/sp/package/")
@Api(tags = "专项计划-专项计划包")
public class SpPlanPackageController implements WorkflowController {

    @Autowired
    private SpPlanPackageService packageService;

    @Autowired
    private SpPlanService planService;

    @Autowired
    private SpPlanPackageFilter filter;

    @Autowired
    private SystemFeignService systemFeignService;

    @GetMapping("/selectOne")
    @ApiOperation("专项计划包详情查询")
    @ApiImplicitParam(name = "pkgId", value = "专项计划包id", required = true)
    public Response<SpPlanPackageVO> selectOne(@CasUser @ApiIgnore EamUser user, @RequestParam String pkgId) {
        SpPlanPackageVO detail = ADOConnection.runTask(user.getCurrDs(), packageService, "selectOne", SpPlanPackageVO.class, pkgId);
        Assert.isTrue(detail != null && !detail.getDeleteFlag(), "专项计划包不存在");
        return Response.ok("查询成功", detail);
    }

    @GetMapping("/queryList")
    @ApiOperation("专项计划包分页查询")
    public Response<PageInfo<SpPlanPackageVO>> queryList(@CasUser @ApiIgnore EamUser user, SpPlanPackageQuery query) {
        OrderByInterceptor.setOrderBy(query.getOrders());
        PageHelper.startPage(query.getPage(), query.getPageSize());
        List<SpPlanPackageVO> list = ADOConnection.runTask(user.getCurrDs(), packageService, "queryList", List.class, query);
        return Response.ok("查询成功", new PageInfo<>(list));
    }

    @GetMapping("/plan/queryList")
    @ApiOperation("专项计划包-专项计划分页查询")
    public Response<PageInfo<SpPlanPackageVO>> queryPlanList(@CasUser @ApiIgnore EamUser user, SpPlanQuery query) {
        List<SpPlanPackageVO> list = ADOConnection.runTask(user.getCurrDs(), packageService, "queryPlanList", List.class, query);
        return Response.ok("查询成功", new PageInfo<>(list));
    }

    @GetMapping("/plan/groupByDept")
    @ApiOperation("专项计划包-专项计划根据组织分组查询")
    public Response<List<Map<String, Object>>> groupByDept(@CasUser @ApiIgnore EamUser user, String pkgId) {
        List<Map<String, Object>> list = ADOConnection.runTask(user.getCurrDs(), packageService, "groupByPkgId", List.class, pkgId);
        return Response.ok("查询成功", list);
    }

    @GetMapping("/plan/getById")
    @ApiOperation("专项计划包-专项计划详情查询")
    public Response<SpPlanVO> getPlanById(@CasUser @ApiIgnore EamUser user, String planId) {
        SpPlanVO plan = ADOConnection.runTask(user.getCurrDs(), packageService, "getPlanById", SpPlanVO.class, planId);
        return Response.ok("查询成功", plan);
    }

    @GetMapping("/plan/deleteById")
    @ApiOperation("专项计划包-专项计划级联删除")
    public Response<Boolean> deletePlanById(@CasUser @ApiIgnore EamUser user, String planId) {
        Boolean flag = ADOConnection.runTask(user.getCurrDs(), packageService, "deletePlanById", Boolean.class, planId);
        return Response.ok("删除成功", flag);
    }

    @PostMapping("/saveOrUpdate")
    @ApiOperation("新增或修改专项计划包")
    public Response<SpPlanPackageVO> saveOrUpdate(@CasUser @ApiIgnore EamUser user, @RequestBody SpPlanPackageVO detail) {
        detail = ADOConnection.runTask(user.getCurrDs(), packageService, "saveOrUpdate", SpPlanPackageVO.class, detail);
        return Response.ok("操作成功", detail);
    }

    @Deprecated
    //@PostMapping("/submit")
    @ApiOperation("提交专项计划包, 可重复提交（工作流不会重复创建）")
    public Response<SpPlanPackageVO> submit(@CasUser @ApiIgnore EamUser user, @RequestBody SpPlanPackageVO detail) {
        detail = ADOConnection.runTask(user.getCurrDs(), packageService, "submit", SpPlanPackageVO.class, detail);
        return Response.ok("提交成功", detail);
    }

    @GetMapping("/deleteById")
    @ApiOperation("根据id删除专项计划包")
    @ApiImplicitParam(name = "pkgId", value = "专项计划包id", required = true)
    public Response<String> deleteById(@CasUser @ApiIgnore EamUser user, @RequestParam String pkgId) {
        ADOConnection.runTask(user.getCurrDs(), packageService, "deleteById", String.class, pkgId);
        return Response.ok("删除成功", pkgId);
    }

    @Override
    @ApiOperation("修改业务状态, 非前端接口")
    public Response updateState(@RequestBody RedirectQuery redirectQuery) {
        log.info("工作流更新： {}", JSON.toJSONString(redirectQuery));
        ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), packageService, "workflowUpdate", Boolean.class, redirectQuery);
        return Response.success();
    }

    @Override
    @ApiOperation("结束时的修改方法, 非前端接口")
    public Response updateEnd(@RequestBody RedirectQuery redirectQuery) {
        log.info("工作流完结 ： {}", JSON.toJSONString(redirectQuery));
        SpPlanPackageVO vo = JSON.parseObject(redirectQuery.getFormVariables(), SpPlanPackageVO.class);
        ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), packageService, "auditFinish", SpPlanPackageVO.class, vo);
        return Response.success();
    }

    @Override
    @ApiOperation("作废时的修改方法, 非前端接口")
    public Response updateCancel(@RequestBody RedirectQuery redirectQuery) {
        log.info("updateCancel = {}", JSON.toJSONString(redirectQuery));
        return Response.success();
    }

    /**
     * 自动过滤
     * 点击工作流提交的时候，业务需要编写此过滤器，返回过滤后的人员信息
     * @param submitterParam
     */
    @PostMapping("/{taskN}/submitter")
    public Response<List<WorkflowUser>> submitter(@PathVariable String taskN, @RequestBody SubmitterParam submitterParam) {
        log.info("专项计划包设置审批人入参: {}", JSON.toJSONString(submitterParam));
        List<WorkflowUser> users = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), filter, taskN + "Submitter", List.class, submitterParam);
        return Response.ok(users);
    }

    /**
     * 选人组件过滤
     * 点击选人组件时，业务需要编写此过滤器，返回过滤后的人员信息
     *
     * @param submitterParam
     */
    @PostMapping("/{taskN}/handler")
    public Response<List<WorkflowUser>> handler(@PathVariable String taskN, @RequestBody SubmitterParam submitterParam) {
        log.info("专项计划包选人组件过滤入参: {}", JSON.toJSONString(submitterParam));
        List<WorkflowUser> users = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), filter, taskN + "Handler", List.class, submitterParam);
        return Response.ok(users);
    }

}
